/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.gui.customclasses;

import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Federico Improta
 */
public class ImageComboBoxRenderer extends JLabel implements ListCellRenderer {

	public ImageComboBoxRenderer() {
		setOpaque(true);
		//setHorizontalAlignment(LEFT);
		setVerticalAlignment(CENTER);
	}

	/*
	 * This method finds the image and text corresponding
	 * to the selected value and returns the label, set up
	 * to display the text and image.
	 */
	@Override
	public Component getListCellRendererComponent(
			  JList list,
			  Object value,
			  int index,
			  boolean isSelected,
			  boolean cellHasFocus) {
		//Get the selected index. (The index param isn't
		//always valid, so just use the value.)

		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}

		//Set the icon and text.  If icon was null, say so.
		if (value instanceof ImageOwnerObject) {
			ImageOwnerObject ioValue = (ImageOwnerObject) value;
			ImageIcon icon = ioValue.getImageIcon();
			setIcon(icon);
			setText(ioValue.getLabel());
			setFont(list.getFont());
		} else {
			setIcon(null);
			setText(value.toString());
		}


		return this;
	}
}
